//
//  ZJMainTabBarViewController.m
//  YueLoveBaby
//
//  Created by 张敬 on 2018/4/21.
//  Copyright © 2018年 张敬. All rights reserved.
//

#import "TJMainTabBarViewController.h"


#pragma mark - new

#import "AdoptPigViewController.h"
#import "MeatMainListViewController.h"
#import "DiscoveryViewController.h"
#import "MeViewController.h"
#import "TJMeViewController.h"

@interface TJMainTabBarViewController ()

@property (nonatomic, strong, nonnull) NSArray *controllsArr;
@property (nonatomic, strong, nonnull) NSArray *titleArr;
@property (nonatomic, strong, nonnull) NSArray *tabbarNomalImageArr;
@property (nonatomic, strong) NSArray *tabbarSelectedImageArr;

@end

@implementation TJMainTabBarViewController

#pragma mark - lifecycle methods

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    for (UIView *subView in self.tabBar.subviews) {
        if (![subView isKindOfClass:[TJTabBar class]]) {
            [subView removeAllSubviews];
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KWhiteColor;
    [self configTabBarVC];
    [self configTabBar];

    self.myTabBar =  [[TJTabBar alloc] init];
    
    __weak typeof(self) weakself = self;
    self.myTabBar.block = ^(int tag) {
       weakself.selectedViewController = weakself.viewControllers[tag];
    };
    
    self.myTabBar.middleBlock = ^{
//
//        XXQRCodeViewController *vc = [[XXQRCodeViewController alloc] init];
//        BaseNavViewController *nav = [[BaseNavViewController alloc] initWithRootViewController:vc];
//        [weakself presentViewController:nav animated:YES completion:nil];
    };
    

    [self.view addSubview:self.myTabBar];
    [self addNotifications];
}


#pragma mark - 小红点
- (void)showOrderRedPointNotification
{
    //显示订单红点
//    XXTGTabBarItem * item = [self.myTabBar viewWithTag:1];
//    if (item) {
//        item.redPointView.hidden = NO;
//    }
}

- (void)hideOrderRedPointNotification
{
    //显示订单红点
//    XXTGTabBarItem * item = [self.myTabBar viewWithTag:1];
//    if (item) {
//        item.redPointView.hidden = YES;
//    }
}

- (void)changeBadgeNumberNotification:(NSNotification *)notification
{
//    NSDictionary * infoDict = [notification userInfo];
//    NSInteger badge = [infoDict[@"badge"] integerValue];
//
//    XXTGTabBarItem * item = [self.myTabBar viewWithTag:2];
//
//    if (badge > 99) {
//        item.badgeLabel.text = @"99";
//        item.badgeLabel.hidden = NO;
//    }else if (badge == 0) {
//        item.badgeLabel.text = @"";
//        item.badgeLabel.hidden = YES;
//    } else {
//        item.badgeLabel.text = [NSString stringWithFormat:@"%ld",badge];
//        item.badgeLabel.hidden = NO;
//    }
}

#pragma mark - configTabBarVC and tabbar

- (void)configTabBarVC
{
    for (int i = 0; i< self.controllsArr.count; i++) {
        UIViewController *controls = self.controllsArr[i];
       // BaseNavViewController *nav = [[BaseNavViewController alloc] initWithRootViewController:controls];
       // [nav setNavigationBarHidden:YES animated:NO];
       // [self addChildViewController:nav];
        [self addChildViewController:controls];
    }
}

- (void) configTabBar
{
    [[UITabBar appearance] setShadowImage:[UIImage getImageWithColor:KClearColor size:self.tabBar.frame.size]];
    [[UITabBar appearance] setBackgroundImage:[UIImage getImageWithColor:KClearColor size:self.tabBar.frame.size]];
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    self.selectedViewController = self.viewControllers[selectedIndex];
    [self.myTabBar dealJumpTapOperationByIndex:selectedIndex];
}


#pragma mark - setter and getter

- (NSArray *)controllsArr
{
    if (!_controllsArr) {
        
        AdoptPigViewController *homeControl = [[AdoptPigViewController alloc] init];
        MeatMainListViewController *meatCityControl = [[MeatMainListViewController alloc] init];
        DiscoveryViewController *discoveryControl = [[DiscoveryViewController alloc] init];
        //MeViewController *meControl = [[MeViewController alloc] init];
        TJMeViewController *meControl = [[TJMeViewController alloc] init];
        _controllsArr = @[homeControl, meatCityControl, discoveryControl, meControl];
    }
    return _controllsArr;
}


#pragma mark - addNotification methods
- (void)addNotifications{
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeBadgeNumberNotification:) name:kChangeBadgeNumberNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showOrderRedPointNotification) name:kShowOrderRedPointNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideOrderRedPointNotification) name:kHideOrderRedPointNotification object:nil];

}

- (void)removeNotifications{
    
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:kChangeBadgeNumberNotification object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:kShowOrderRedPointNotification object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:kHideOrderRedPointNotification object:nil];
}

- (void)dealloc{
    
    [self removeNotifications];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
